/**
 * 测试报告用例转换器
 *
 * Created by snowingsea on 2021/01/20.
 */
const utility = require('../../../utility');
const BaseTestCaseTransformer = require('./../base-report-test-case-transformer')();
class ReportTestCaseTransformer extends BaseTestCaseTransformer {
  getAreas() {
    function getLeafs(arr, leafs = []) {
      for (let i = 0; i < arr.length; i += 1) {
        const item = arr[i];
        if (item.children && item.children.length) {
          getLeafs(item.children, leafs);
        } else {
          leafs.push(item);
        }
      }
      return leafs;
    }

    const leafs = getLeafs([
      ...this.paramVariablesData,
      ...this.globalVariablesData,
    ]);
    if (leafs.length && !leafs.find((l) => l.data && l.data['@value'])) {
      const key = utility.randomNum(0, leafs.length - 1);
      leafs[key].data = leafs[key].data || {};
      leafs[key].data['@value'] = '0';
    }
    return [
      this.paramVariablesData,
      this.globalVariablesData,
      this.staticsData,
      this.stubsData,
      this.returnValueData,
      this.globalOutputData,
      this.staticsOutputData,
    ];
  }
}

module.exports = ReportTestCaseTransformer;
